<template>
  <div class="channel-edit">
    <van-cell title="我的频道" :border="false">
      <van-button
        size="mini"
        round
        type="danger"
        plain
        @click="isEdit=!isEdit"
      >{{isEdit?'完成':'编辑'}}</van-button>
    </van-cell>
    <van-grid :gutter="10">
      <van-grid-item
        class="channel-item"
        v-for="(channel,index) in userChannels"
        :key="channel.id"
        @click="userChannelsClick(channel,index)"
      >
        <template slot="default">
            <span :class="['text-wrap',{'text-wrap-active':activeIndex===index}]">{{channel.name}}</span>
            <van-icon name="clear" v-show="isEdit&&channel.name!=='推荐'"></van-icon>
        </template>
      </van-grid-item>
              <!-- :text="value.name"
        :icon="!isEdit?'clear':''" -->
    </van-grid>
    <van-cell title="频道推荐" :border="false"></van-cell>
    <van-grid :gutter="10">
      <van-grid-item
        class="channel-item van-ellipsis"
        v-for="value in recommendChannels"
        :key="value.id"
        :text="value.name"
        @click="recommendChannelClick(value)"
      />
    </van-grid>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getAllChannels, addUserChannel } from '@/api/article'
export default {
  name: 'ChannelEdit',
  components: {},
  props: {
    userChannels: {
      type: Array,
      required: true
    },
    activeIndex: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      allChannels: [], // 所有频道
      // 是否是编辑状态
      isEdit: false
    }
  },
  computed: {
    ...mapState(['user']),
    recommendChannels () {
      const { allChannels, userChannels } = this
      return allChannels.filter(item => userChannels.findIndex(userItem => userItem.id === item.id) === -1
      )
    }
  },
  watch: {},
  created () {
    this.loadAllChannels()
  },
  mounted () {},
  methods: {
    async loadAllChannels () {
      try {
        const { data } = await getAllChannels()
        this.allChannels = data.data.channels
      } catch (err) {
        this.$toast('获取所有频道列表失败')
      }
    },
    async recommendChannelClick (value) {
      this.userChannels.push(value)
      if (!this.user) return window.localStorage.setItem('channels', JSON.stringify(this.userChannels))
      try {
        const res = await addUserChannel([{
          id: value.id,
          seq: this.userChannels.length
        }])
        console.log(res)
      } catch (err) {
        console.log(err)
        this.$toast('添加频道失败')
      }
    },
    userChannelsClick (channel, index) {
    //   if (!this.user) return window.localStorage.setItem('channels', JSON.stringify(this.userChannels))
      if (this.isEdit) {
        // 删除操作
        this.$emit('deleteMyChannels', channel.id)
      } else {
        this.$emit('update:active-index1', index)
        this.$emit('close-popup')
      }
    }
  }
}
</script>

<style scoped lang="less">
    .channel-edit {
        .channel-item {
            height: 86px;
                /deep/ .van-grid-item__content {
                position: relative;
                background-color: #f5f5f6;
                .van-grid-item__text {
                    color: #222;
                    font-size: 28px;
                }
                .text-wrap{
                    color: #222;
                    font-size: 28px;
                }
                .text-wrap-active{
                    color: #ff0000;
                }
                .van-icon-clear{
                    position: absolute;
                    top: -15px;
                    right: -15px;
                    font-size: 30px;
                    color: #cacaca;
                }
            }
        }
        [class*=van-hairline]::after{
            border: none;
        }
    }
</style>
